# -*- codeing = utf-8 -*-
# @Time : 2021/3/21 0021 10:52
# @Author : Alan Franklin
# @File : UI_checkdetails.py
# @Software: PyCharm
from tkinter import *
from tkinter.messagebox import *
from UI_globalstack import *
from PIL import ImageTk
from UI_functions import *
import os
global imagefile


class DetailsInterface(object):
    def __init__(self, master=None, dir=None):
        self.root = master
        self.root.geometry('640x720')
        self.exp_dir = StringVar()
        self.exp_dir = dir
        self.CreatePage()

    def CreatePage(self):
        self.page = Frame(self.root)
        self.page.pack()

        Button(self.page, text='返回', height=2, command=self.GoBack).grid(row=1, sticky=W)

        global imagefile
        labels_dir, pic_dir = self.GetDir()
        imagefile = ImageTk.PhotoImage(file=pic_dir)
        canvas = Canvas(self.page, bg='white', height=480, width=640)
        canvas.create_image(0, 0, anchor='nw', image=imagefile)
        canvas.grid(row=2, sticky=W)

        text = Text(self.page, height=10)
        text.grid(row=3, sticky=W)
        text.insert('end', '器械种类以及图中四角位置坐标：\n')
        f = open(labels_dir, 'r')
        for line in f.readlines():
            text.insert('end', line+'\n')
        f.close()

    def GetDir(self):
        labels_dir = ''
        pic_dir = ''
        the_dir = self.exp_dir.get()
        get_dir = os.listdir(the_dir)
        for i in get_dir:
            sub_dir = os.path.join(the_dir, i)
            if os.path.isdir(sub_dir):
                labels_dir = os.path.join(sub_dir, os.listdir(sub_dir)[0])
            else:
                pic_dir = sub_dir
        return labels_dir, pic_dir

    def GoBack(self):
        self.page.destroy()
        gs.mystack.Pop().CreatePage()
        gs.mystack.Push(self)